package com.guomao.controller;


import com.guomao.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author cjrh2
 */

//@SessionAttributes(names = {"msg1", "msg2"})
@SessionAttributes(types = String.class)
@Controller
public class TestController {

    @RequestMapping("/header")
    public String getHeader(@RequestHeader("User-Agent") String agent) {
        System.out.println(">>>>>>>>>>>" + agent);
        return "main";
    }

    @RequestMapping("/set")
    public void setCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("myname", "zhaoliu");
        response.addCookie(cookie);

//        return "main";
    }

/*    @RequestMapping("/get")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals("myname")) {
                    System.out.println(c.getValue());
                }
            }
        }
        return "main";
    }*/

    @RequestMapping("/get")
    public String getCookie(@CookieValue("myname") String myname) {
        return "main";
    }


    @RequestMapping("/set1")
    public String setValue1(HttpServletRequest request) {
        request.setAttribute("name", "admin");
        return "main";
    }

    @RequestMapping("/set2")
    public ModelAndView setValue2() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("address", "福建省漳州市");
        mav.setViewName("main");
        return mav;
    }


    @RequestMapping("/set3")
    public String setValue3(Model m) {//接口
        m.addAttribute("province", "广东省");

        m.addAttribute("classname", m.getClass().getName());
//        return "WEB-INF/pages/main.jsp";
        return "main";
    }


    @RequestMapping("/set4")
    public String setValue4(Map<String, Object> m) {//Map接口

        m.put("price", 7999);
        m.put("classname", m.getClass().getName());
        return "main";
    }


    @RequestMapping("/set5")
    public String setValue5(ModelMap m) {
        m.addAttribute("classname", m.getClass().getName());
        m.addAttribute("hostname", "localhost");
        return "main";
    }


    @RequestMapping("/set6")
    public String setValue6(HttpSession session) {
        session.setAttribute("name", "tianqi");
        return "main";
    }

    @RequestMapping("/set7")
    public String setValue7(HttpSession session, Model m) {

        Object name = session.getAttribute("name");

        m.addAttribute("newname", name);
        return "main";
    }


    @RequestMapping("/set8")
    public String setValue8(HttpSession session, HttpServletRequest request) {
        ServletContext context1 = session.getServletContext();
//        ServletContext context2 = request.getServletContext();
//        System.out.println(context2 == context1);

        context1.setAttribute("number", 9999);
        return "main";
    }


    @RequestMapping("/set9")
    public String setValue9(HttpSession session, HttpServletRequest request) {
        Object number = request.getServletContext().getAttribute("number");
        System.out.println("number" + number);
        return "main";
    }

    @RequestMapping("/set10")
    public String setValue10(Model m) {

        m.addAttribute("msg1", "hello springmvc");

        return "main";
    }

    @RequestMapping("/get11")
    public String getValue11(HttpSession session) {

        Object msg1 = session.getAttribute("msg1");

        System.out.println("msg1:" + msg1);

        return "main";
    }


    @RequestMapping("/set12")
    public void setValue12(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/user.jsp").forward(request, response);
    }

    @RequestMapping("/set13")
    public void setValue13(HttpServletResponse response) throws IOException {
        response.sendRedirect("https://www.baidu.com");
    }


    @RequestMapping("/getuser")
    @ResponseBody
    public User getUser() {
        User user = new User("root", 20, "男", "广东省广州市");
        return user;
    }


}
